//
// Created by 君未言 on 2022/5/8.
//
#include "Thread.h"
#include "CurrentThread.h"
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>

namespace detail {

    pid_t gettid() {
        return static_cast<pid_t>(::syscall(SYS_gettid));
    }

}  // namespace detail

void CurrentThread::cacheTid() {
    if (t_cachedTid == 0) {
        t_cachedTid = detail::gettid();
        t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);
    }
}

bool CurrentThread::isMainThread() {
    return tid() == ::getpid();
}

